package com.sh.data.engine.domain.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.sh.data.engine.domain.api.model.domain.DataApiTopUserReqDomain;
import com.sh.data.engine.domain.api.service.DataApiUserStatisticsService;
import com.sh.data.engine.repository.dao.api.DataApiUserStatisticsMapper;
import com.sh.data.engine.repository.dao.api.entity.DataApiUserStatisticsEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author : fuchen
 * @date : 2022/2/10 16:15
 */
@Service
public class DataApiUserStatisticsServiceImpl
    extends ServiceImpl<DataApiUserStatisticsMapper, DataApiUserStatisticsEntity>
    implements DataApiUserStatisticsService {

    @Override
    public Long getTotalCountByProjectIdAndUserId(Long projectId, String userId) {

        LambdaQueryWrapper<DataApiUserStatisticsEntity> eq =
            new LambdaQueryWrapper<DataApiUserStatisticsEntity>()
                .eq(DataApiUserStatisticsEntity::getProjectId, projectId)
                .eq(DataApiUserStatisticsEntity::getUserId, userId);

        List<DataApiUserStatisticsEntity> multi = this.list(eq);
        long sum =
            multi.stream()
                .filter(Objects::nonNull)
                .mapToLong(DataApiUserStatisticsEntity::getTotalCount)
                .sum();
        return sum;
    }

    @Override
    public Long getFailCountByProjectIdAndUserId(Long projectId, String userId) {

        LambdaQueryWrapper<DataApiUserStatisticsEntity> eq =
            new LambdaQueryWrapper<DataApiUserStatisticsEntity>()
                .eq(DataApiUserStatisticsEntity::getProjectId, projectId)
                .eq(DataApiUserStatisticsEntity::getUserId, userId);

        List<DataApiUserStatisticsEntity> multi = this.list(eq);
        long sum =
            multi.stream()
                .filter(Objects::nonNull)
                .mapToLong(DataApiUserStatisticsEntity::getFailCount)
                .sum();
        return sum;
    }

    @Override
    public List<DataApiTopUserReqDomain> getDataApiTopUserReqDomainList(Long projectId) {

        QueryWrapper<DataApiUserStatisticsEntity> queryChangeReasonWrapper =
            new QueryWrapper<DataApiUserStatisticsEntity>()
                .select("sum(total_count) as total , username as username,user_id as userId")
                .eq("project_id", projectId)
                .groupBy("user_id", "username")
                .orderByDesc("total")
                .last("limit 10");
        List<Map<String, Object>> maps = this.listMaps(queryChangeReasonWrapper);

        List<DataApiTopUserReqDomain> r1 = Lists.newArrayList();

        for (Map<String, Object> p : maps) {
            Object total1 = p.get("total");
            if (total1 == null) {
                total1 = p.get("TOTAL");
            }
            if (Objects.nonNull(total1)) {
                Long total = Long.valueOf(total1.toString());
                Object username1 = p.get("username");
                if (username1 == null) {
                    username1 = p.get("USERNAME");
                }
                if (username1 == null) {
                    username1 = p.get("userName");
                }
                String username = String.valueOf(username1);
                Object userId1 = p.get("userid");
                if (userId1 == null) {
                    userId1 = p.get("USERID");
                }
                if (userId1 == null) {
                    userId1 = p.get("userId");
                }
                DataApiTopUserReqDomain dataApiTopUserReqDomain = new DataApiTopUserReqDomain();
                dataApiTopUserReqDomain.setUserId(userId1.toString());
                dataApiTopUserReqDomain.setUsername(username);
                dataApiTopUserReqDomain.setTotal(total);
                r1.add(dataApiTopUserReqDomain);
            }
        }
        return r1;
    }
}
